<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends CommonController {


	//商品详情页面
	public function showgoods(){
		$goodsid = I("get.goodsid");
		$Goods = M('Goods');
		$goodsdata = $Goods->where("id ={$goodsid}")->find();
		$twosort = M('Twosort');
		$goodsdata['twosortname'] = $twosort->where("id={$goodsdata['twosortid']}")->getField('twosortname');
		$goodsdata['goodsimg'] = json_decode($goodsdata['goodsimg'],true);
		$this->assign('goodsdata',$goodsdata);
		if (!empty($_SESSION['USERID'])) {
			$this->getcolmsg();
			$this->getshopcart();
			//加入到用户足迹信息表中
			$this->addfoot();
		}
		$this->loadGoodsul();
		$this->loadReleategoods($goodsdata['twosortid']);
		$this->loadReview();

		
		$this->display("product");
	}

	//加载商品评论
	public function loadReview(){
		$goodsid = I("get.goodsid");
		$Review =M("Review");
		$reviewcount = $Review->where("goodsid ={$goodsid}")->count();
		$reviewlist = $Review->where("goodsid ={$goodsid}")->select();
		$ratingsum = 0;
		$reviewlistdata = array();
		$User = M("User");
		foreach ($reviewlist as $value) {
			$value['username'] = $User->where("id={$value['userid']}")->getField('username');
			$ratingsum = $value['rating']+$ratingsum;
			$reviewlistdata[ ] = $value;
		}
		$ratingaverage = round($ratingsum/$reviewcount);
		$this->assign('ratingaverage',$ratingaverage);
		$this->assign("reviewcount",$reviewcount);
		$this->assign("reviewlistdata",$reviewlistdata);
	}
	//加载相关产品
	public function loadReleategoods($twosortid){
		$Goods = M('Goods');
		$Review =M("Review");
		$Releategoods=$Goods->where("twosortid={$twosortid}")->limit(4)->select();
		$Releategoodslist = array(); 
		foreach ($Releategoods as $value) {
			
			$value['goodsimg'] = json_decode($value['goodsimg'],true);
			$ratingsum = 0;
			$reviewcount = $Review->where("goodsid ={$value['id']}")->count();
			if ($reviewcount==0) {
				$value['ratingaverage'] = 0;
			}else{
				$reviewlist = $Review->where("goodsid ={$value['id']}")->select();
				foreach ($reviewlist as $val) {
					$ratingsum = $val['rating']+$ratingsum;
				}
				$value['ratingaverage'] = round($ratingsum/$reviewcount);
			}
			$value['percent'] =($value['goodsnow'] - $value['goodsorig'])/$value['goodsorig']*100;
			$Releategoodslist[]=$value;
		}
		$this->assign("Releategoodslist",$Releategoodslist);
	}
	//展示商品二级分类页面
	public function showcategory(){
		$twosortid = I("get.twosortid");
		$twosort = M('Twosort');
		$twosortname = $twosort->where("id={$twosortid}")->getField('twosortname');
		$sortid = $twosort->where("id={$twosortid}")->getField('sortid');
		$this->loadGoodsul();
		$this->loadsortmsg();
		$this->loadnewgoods();
		$this->laodgoodslist($twosortid);
		if (!empty($_SESSION['USERID'])) {
			$this->getcolmsg();
			$this->getshopcart();
		}
		$this->assign("twosortname",$twosortname);
		$this->assign("sortid",$sortid);
		$this->display("category");
	}
	
	//加载商品二级分类页面的左边 
	public function loadsortmsg(){
		$sort = 	M("Sort");
		$twoSort = M("Twosort");
		$Sortdata = $sort->select();
		$sortlist = array();
		foreach ($Sortdata as $val) {
			$Twosortdata = $twoSort->where("sortid = {$val['id']}")->select();
			$val['twosortdata'] = $Twosortdata;
			$sortlist[ ] = $val;
		}
		$this->assign("sortlistleft",$sortlist);

	}
	//加载新商品
	public function loadnewgoods(){
		$Goods = M("Goods");
		$newgoodsdata = $Goods->order("id desc")->limit(5)->select();
		$Review =M("Review");
		$newgoodslist = array(); 
		foreach ($newgoodsdata as $value) {
			$value['goodsimg'] = json_decode($value['goodsimg'],true);
			$ratingsum = 0;
			$reviewcount = $Review->where("goodsid ={$value['id']}")->count();
			if ($reviewcount==0) {
				$value['ratingaverage'] = 0;
			}else{
				$reviewlist = $Review->where("goodsid ={$value['id']}")->select();
				foreach ($reviewlist as $val) {
					$ratingsum = $val['rating']+$ratingsum;
				}
				$value['ratingaverage'] = round($ratingsum/$reviewcount);
			}
			$newgoodslist[]=$value;
		}
		$this->assign("newgoodslist",$newgoodslist);
	}

	//加载商品列表
	public function laodgoodslist($towsortid){
		$Goods = M("Goods");
		$goodsdata=$Goods->where("twosortid = {$towsortid}")->select();
		$Review =M("Review");
		$goodslist = array(); 
		foreach ($goodsdata as $value) {
			$value['goodsimg'] = json_decode($value['goodsimg'],true);
			$value['percent'] =($value['goodsnow'] - $value['goodsorig'])/$value['goodsorig']*100;
			$ratingsum = 0;
			$reviewcount = $Review->where("goodsid ={$value['id']}")->count();
			if ($reviewcount==0) {
				$value['ratingaverage'] = 0;
			}else{
				$reviewlist = $Review->where("goodsid ={$value['id']}")->select();
				foreach ($reviewlist as $val) {
					$ratingsum = $val['rating']+$ratingsum;
				}
				$value['ratingaverage'] = round($ratingsum/$reviewcount);
			}
			$goodslist[]=$value;
		}
		$this->assign("goodslist",$goodslist);
		//分页
		// $count = $Goods->where("twosortid = {$towsortid}")->count();
		// $Page    = new \Think\Page($count,1);// 实例化分页类 传入总记录数和每页显示的记录数(1)
		// $Page->setConfig('header',"<span>共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</span>");
		// $Page->setConfig('first','首页');
		// $Page->setConfig('last','共%TOTAL_PAGE%页');
		// $Page->setConfig('prev','<<');
		// $Page->setConfig('next','>>');
		// $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
		// $show  = $Page->show();// 分页显示输出
		// // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		// $list = $Goods->where("twosortid = {$towsortid}")->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->select();
		// $goodslist = array(); 
		// foreach ($list as $value) {
		// 	$value['goodsimg'] = json_decode($value['goodsimg'],true);
		// 	$value['percent'] =($value['goodsnow'] - $value['goodsorig'])/$value['goodsorig']*100;
		// 	$goodslist[]=$value;
		// }
		// $this->assign('show',$show);// 赋值分页输出
		// $this->assign("goodslist",$goodslist);
	}
	
	//商品评论
	public function review(){
		$userid = $_SESSION['USERID'];
		if (empty($userid)) {
			$this->error("请登录后再评论商品");
		}
		
		$rules = array(
			array('reviewcnt','require','评论内容不能为空！'), // 评论内容必须
			array('rating','require','商品评级不能为空！') // 商品评级必须
		);
		$Review =M("Review");
		$data=$Review->validate($rules)->create();
		if (!$data) {
			$this->error($Review->getError()); // 如果创建失败 表示验证没有通过 输出错误提示信息
		}else{
			$data['reviewcnt'] = I("post.reviewcnt",'','');
			$data['goodsid'] = I("get.goodsid");
			$data['userid'] = $userid; 
			$data['reviewtime'] = time();
			$res=$Review->add($data);
			if (!$res) {
				$this->error("评论商品失败");
			}else{
				$this->success("评论商品成功");
			}
		}
		
	}

	public function addfoot(){
		$Foot = M("Foot");
		$userid = $_SESSION['USERID'];
		$goodsid = I("get.goodsid");
		$Foot = M("Foot");
		$footmsg = $Foot->where("userid ={$userid} AND goodsid = {$goodsid} ")->find();
		if ($footmsg) {
			$data = array(
				"foottime"=>time()
			);
			$res=$Foot->where("userid ={$userid} AND goodsid = {$goodsid} ")->save($data);
		}else{
			$data = array(
				"userid"=>$userid,
				"goodsid"=>$goodsid,
				"foottime"=>time()
			);
			$res = $Foot->add($data);
		}
		
	}
}